package com.zzy.controller;

import com.zzy.model.District;
import com.zzy.service.DistrictService;
import com.zzy.util.json.util_json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("districtController")
public class DistrictController {

    @Autowired
    private DistrictService districtService;


    @RequestMapping(value="/getDistrict.do")
    public String getDistrict(HttpServletRequest request,HttpServletResponse response){
        District District =  districtService.getDistrictById(3);
        request.setAttribute("district",District);
        return "district/district.jsp";
    }

    /**
     * 获取 唯一数据
     * @param district
     * @param request
     * @param response
     */
    @RequestMapping(value="/getDistrictByParam.do")
    public void getDistrictByParam(District district ,HttpServletRequest request,HttpServletResponse response){
        district.setXzid("110100");//根据条件 获取唯一数据
        District district2 = districtService.getModelByParam(district);
        util_json.jsonPrintModel(district2,response);
    }

    /**
     * 根据查询条件获取list
     * @param district
     * @param request
     * @param response
     */
    @RequestMapping(value="/getLitsByParam.do")
    public void getLitsByParam(District district ,HttpServletRequest request,HttpServletResponse response){
        district.setMergername("北京");//根据条件 获取唯一数据
        List<District> list = districtService.getListByParam(district);
        util_json.jsonPrintList(list,response);
    }

    /**
     * 根据查询条件   分页  获取list
     * @param district
     * @param request
     * @param response
     */
    @RequestMapping(value="/getListByPage.do")
    public void getListByPage(District district ,HttpServletRequest request,HttpServletResponse response){
        //district.setMergername("北京");//根据条件 获取唯一数据
        int page = 2; int rows = 3;
        Map map = new HashMap();
        map.put("mergername","北京");
        map.put("page",(page-1)*rows);
        map.put("rows",rows);
        List<District> list = districtService.getListByPage(map);
        util_json.jsonPrintList(list,response);
    }
    @RequestMapping(value="/saveObj.do")
    public void saveObj(District district ,HttpServletRequest request,HttpServletResponse response){
        //district.setId(8888);id 不能要 已经设置为 自动增长 想要的话 在xml 增加 插入 id 列
        district.setCitycode("aaa");//根据条件 获取唯一数据
        districtService.saveObj(district);
        util_json.jsonPrintModel("{\"success\":\"保存成功！\"}",response);
    }
    @RequestMapping(value="/updateObj.do")
    public void updateObj(District district ,HttpServletRequest request,HttpServletResponse response){
        district.setMergername("eee");//根据条件 获取唯一数据
        district.setId(8892);//自己找的数据id
        district.setPinyin("qqq");
        districtService.updateObj(district);
        util_json.jsonPrintModel("{\"success\":\"修改成功！\"}",response);
    }
    @RequestMapping(value="/deleteObjById.do")
    public void deleteObjById(District district ,HttpServletRequest request,HttpServletResponse response){
        districtService.deleteObjById(8892);
        util_json.jsonPrintModel("{\"success\":\"删除成功！\"}",response);
    }





}
